//
//  LeftViewController.m
//  QuickYi
//
//  Created by 张贞献 on 16/5/9.
//  Copyright © 2016年 张贞献. All rights reserved.
//

#import "LeftViewController.h"

#define ORIGINAL_MAX_WIDTH 640.0f

@interface LeftViewController ()<UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate, UIActionSheetDelegate, VPImageCropperDelegate>
/** 数组数据 */
@property (nonatomic, strong) NSArray *titleNameArray1;
@property (nonatomic, strong) NSArray *titleNameArray2;


@end

@implementation LeftViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBarHidden = NO;
    self.view.backgroundColor = [[UIColor alloc]initWithRed:196.0/256.0 green:240.0/256.0 blue:226.0/256.0 alpha:1.0];
    
    UITableView *tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    self.tableview = tableview;

    tableview.dataSource = self;
    tableview.delegate  = self;
    
    //tableview.rowHeight = 50;
    //tableview.separatorInset = UIEdgeInsetsMake(0, 16, 0, 60);
   // tableview.separatorColor = [UIColor redColor];
    [self.view addSubview:tableview];
    
    [self setUpUI];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)setUpUI{
    
    self.titleNameArray1 = @[@"名医通申请",@"病例管理",@"关注医生"];
    self.titleNameArray2 = @[@"设置与帮助",@"关于产品",@"分享给好友"];
    
 
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *Identifier = @"Identifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier];
    }

    cell.textLabel.font = [UIFont systemFontOfSize:15.0];

    if (indexPath.section == 0) {
        
        cell.textLabel.text = self.titleNameArray1[indexPath.row];
       
    }else{
        
        cell.textLabel.text = self.titleNameArray2[indexPath.row];
        //cell.accessoryType = UITableViewCellAccessoryCheckmark ;
    }
     // cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    

    return cell;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.tableview.bounds.size.width, 120)];
    //        self.HeadView = view;
    self.tableview.tableHeaderView = view;
    view.backgroundColor = [UIColor grayColor];
    //[[UIColor alloc]initWithRed:196.0/256.0 green:240.0/256.0 blue:226.0/256.0 alpha:1.0];
    UIButton *personButton = [[UIButton alloc] initWithFrame:CGRectMake(18, 58, 65, 65)];
    [view addSubview:personButton];
    NSString* docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString* filePath = [docPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",@"jiawan"]];
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        
        NSData *data = [[NSMutableData alloc] initWithContentsOfFile:filePath];
        [personButton  setBackgroundImage:[UIImage imageWithData:data] forState:UIControlStateNormal];
    }else{
        
        [personButton  setBackgroundImage:[UIImage imageNamed:@"banner"]forState:UIControlStateNormal];
    }
    //添加点击事件
    [personButton addTarget:self action:@selector(selectImage) forControlEvents:UIControlEventTouchUpInside];
    //设置图片圆角
    //       [personButton setImage:[UIImage imageNamed:@"banner"]forState:UIControlStateNormal];
    //       personButton.imageView.clipsToBounds = YES;
    //       personButton.imageView.layer.cornerRadius = personButton.imageView.bounds.size.width * 0.5;
    //    personButton.imageView.userInteractionEnabled = YES;
    //    //添加点击事件
    //    [personButton addTarget:self action:@selector(selectImage) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *userLabel = [[UILabel alloc] initWithFrame:CGRectMake(105,83,150, 14)];

    [view addSubview:userLabel];
    userLabel.font = [UIFont systemFontOfSize:15];
    userLabel.text = @"资料资料资料资料资";
    userLabel.textColor = [UIColor whiteColor];
    UILabel * desLabel = [[UILabel alloc] initWithFrame:CGRectMake(105, 120, 150, 12)];
    [view addSubview:desLabel];
    
    desLabel.font = [UIFont systemFontOfSize:14];
    desLabel.textColor = [UIColor whiteColor];
    desLabel.text = @"12910988";
    
    //添加新闻按钮
    self.personButton= personButton;
    self.userPhoneNumber = userLabel;
    self.desLabel = desLabel;
    
 
    return view;

}


-(void)selectImage{
    //iOS8之前的用法
    UIActionSheet *choiceSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                             delegate:self
                                                    cancelButtonTitle:@"取消"
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:@"拍照", @"从相册中选取", nil];
    [choiceSheet showInView:self.view];
    //iOS8 之后的用法
//    UIAlertController * choiceAlert = [UIAlertController alertControllerWithTitle:@"" message:<#(nullable NSString *)#> preferredStyle:<#(UIAlertControllerStyle)#>];
    /* ios8创建弹框的方法：UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"提示" message:@"输入数字不合理" preferredStyle: UIAlertControllerStyleAlert];
    
    *ios8显示弹框的方法：[self presentViewController:alertVc animated:NO completion:nil];
    
    *ios8添加按钮的方法：[alertVc addAction:[UIAlertAction actionWithTitle:@"取消" style: UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        NSLog(@"点击了取消按钮");
    }]];
    */
}


//#pragma mark VPImageCropperDelegate
//- (void)imageCropper:(VPImageCropperViewController *)cropperViewController didFinished:(UIImage *)editedImage {
//    [self selectPersonImage:editedImage];
//    [cropperViewController dismissViewControllerAnimated:YES completion:^{
//        // TO DO
//    }];
//}
//
//- (void)imageCropperDidCancel:(VPImageCropperViewController *)cropperViewController {
//    [cropperViewController dismissViewControllerAnimated:YES completion:^{
//    }];
//}
//
//#pragma mark UIActionSheetDelegate
//- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
//    if (buttonIndex == 0) {
//        // 拍照
//        if ([self isCameraAvailable] && [self doesCameraSupportTakingPhotos]) {
//            UIImagePickerController *controller = [[UIImagePickerController alloc] init];
//            controller.sourceType = UIImagePickerControllerSourceTypeCamera;
//            if ([self isFrontCameraAvailable]) {
//                controller.cameraDevice = UIImagePickerControllerCameraDeviceFront;
//            }
//            NSMutableArray *mediaTypes = [[NSMutableArray alloc] init];
//            [mediaTypes addObject:(__bridge NSString *)kUTTypeImage];
//            controller.mediaTypes = mediaTypes;
//            controller.delegate = self;
//            [self presentViewController:controller
//                               animated:YES
//                             completion:^(void){
//                                 
//                             }];
//        }
//        
//    } else if (buttonIndex == 1) {
//        // 从相册中选取
//        if ([self isPhotoLibraryAvailable]) {
//            UIImagePickerController *controller = [[UIImagePickerController alloc] init];
//            controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//            NSMutableArray *mediaTypes = [[NSMutableArray alloc] init];
//            [mediaTypes addObject:(__bridge NSString *)kUTTypeImage];
//            controller.mediaTypes = mediaTypes;
//            controller.delegate = self;
//            [self presentViewController:controller
//                               animated:YES
//                             completion:^(void){
//                                 
//                             }];
//        }
//    }
//}
//
//#pragma mark - UIImagePickerControllerDelegate
//- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
//    [picker dismissViewControllerAnimated:YES completion:^() {
//        UIImage *portraitImg = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
//        portraitImg = [self imageByScalingToMaxSize:portraitImg];
//        // 裁剪
//        VPImageCropperViewController *imgEditorVC = [[VPImageCropperViewController alloc] initWithImage:portraitImg cropFrame:CGRectMake(0, 100.0f, self.view.frame.size.width, self.view.frame.size.width) limitScaleRatio:3.0];
//        imgEditorVC.delegate = self;
//        [self presentViewController:imgEditorVC animated:YES completion:^{
//            // TO DO
//        }];
//    }];
//}
//
//- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
//    [picker dismissViewControllerAnimated:YES completion:^(){
//    }];
//}
////
////#pragma mark - UINavigationControllerDelegate
////- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
////}
////
////- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
////    
////}
//
//#pragma mark camera utility
//- (BOOL) isCameraAvailable{
//    return [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
//}
//
//- (BOOL) isRearCameraAvailable{
//    return [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
//}
//
//- (BOOL) isFrontCameraAvailable {
//    return [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront];
//}
//
//- (BOOL) doesCameraSupportTakingPhotos {
//    return [self cameraSupportsMedia:(__bridge NSString *)kUTTypeImage sourceType:UIImagePickerControllerSourceTypeCamera];
//}
//
//- (BOOL) isPhotoLibraryAvailable{
//    return [UIImagePickerController isSourceTypeAvailable:
//            UIImagePickerControllerSourceTypePhotoLibrary];
//}
//- (BOOL) canUserPickVideosFromPhotoLibrary{
//    return [self
//            cameraSupportsMedia:(__bridge NSString *)kUTTypeMovie sourceType:UIImagePickerControllerSourceTypePhotoLibrary];
//}
//- (BOOL) canUserPickPhotosFromPhotoLibrary{
//    return [self
//            cameraSupportsMedia:(__bridge NSString *)kUTTypeImage sourceType:UIImagePickerControllerSourceTypePhotoLibrary];
//}
//
//- (BOOL) cameraSupportsMedia:(NSString *)paramMediaType sourceType:(UIImagePickerControllerSourceType)paramSourceType{
//    __block BOOL result = NO;
//    if ([paramMediaType length] == 0) {
//        return NO;
//    }
//    NSArray *availableMediaTypes = [UIImagePickerController availableMediaTypesForSourceType:paramSourceType];
//    [availableMediaTypes enumerateObjectsUsingBlock: ^(id obj, NSUInteger idx, BOOL *stop) {
//        NSString *mediaType = (NSString *)obj;
//        if ([mediaType isEqualToString:paramMediaType]){
//            result = YES;
//            *stop= YES;
//        }
//    }];
//    return result;
//}
//
//#pragma mark image scale utility
//- (UIImage *)imageByScalingToMaxSize:(UIImage *)sourceImage {
//    if (sourceImage.size.width < ORIGINAL_MAX_WIDTH) return sourceImage;
//    CGFloat btWidth = 0.0f;
//    CGFloat btHeight = 0.0f;
//    if (sourceImage.size.width > sourceImage.size.height) {
//        btHeight = ORIGINAL_MAX_WIDTH;
//        btWidth = sourceImage.size.width * (ORIGINAL_MAX_WIDTH / sourceImage.size.height);
//    } else {
//        btWidth = ORIGINAL_MAX_WIDTH;
//        btHeight = sourceImage.size.height * (ORIGINAL_MAX_WIDTH / sourceImage.size.width);
//    }
//    CGSize targetSize = CGSizeMake(btWidth, btHeight);
//    return [self imageByScalingAndCroppingForSourceImage:sourceImage targetSize:targetSize];
//}
//
//- (UIImage *)imageByScalingAndCroppingForSourceImage:(UIImage *)sourceImage targetSize:(CGSize)targetSize {
//    UIImage *newImage = nil;
//    CGSize imageSize = sourceImage.size;
//    CGFloat width = imageSize.width;
//    CGFloat height = imageSize.height;
//    CGFloat targetWidth = targetSize.width;
//    CGFloat targetHeight = targetSize.height;
//    CGFloat scaleFactor = 0.0;
//    CGFloat scaledWidth = targetWidth;
//    CGFloat scaledHeight = targetHeight;
//    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
//    if (CGSizeEqualToSize(imageSize, targetSize) == NO)
//    {
//        CGFloat widthFactor = targetWidth / width;
//        CGFloat heightFactor = targetHeight / height;
//        
//        if (widthFactor > heightFactor)
//            scaleFactor = widthFactor; // scale to fit height
//        else
//            scaleFactor = heightFactor; // scale to fit width
//        scaledWidth  = width * scaleFactor;
//        scaledHeight = height * scaleFactor;
//        
//        // center the image
//        if (widthFactor > heightFactor)
//        {
//            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
//        }
//        else
//            if (widthFactor < heightFactor)
//            {
//                thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
//            }
//    }
//    UIGraphicsBeginImageContext(targetSize); // this will crop
//    CGRect thumbnailRect = CGRectZero;
//    thumbnailRect.origin = thumbnailPoint;
//    thumbnailRect.size.width  = scaledWidth;
//    thumbnailRect.size.height = scaledHeight;
//    
//    [sourceImage drawInRect:thumbnailRect];
//    
//    newImage = UIGraphicsGetImageFromCurrentImageContext();
//    
//    //pop the context to get back to the default
//    UIGraphicsEndImageContext();
//    return newImage;
//}
//
//////去掉底部线条部分
//-(void)selectPersonImage:(UIImage *)personImage{
//    // -1.获取图片
//    UIImage* image = personImage;
//    // -2.margin
//    CGFloat margin = 5;
//    // -3.上下文大小
//    CGSize size = CGSizeMake(image.size.width + 2 * margin, image.size.height + 2 * margin);
//    // -4.开启图片上下文
//    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
//    // -5.获取图形上下文
//    CGContextRef ctx = UIGraphicsGetCurrentContext();
//    // -6.圆心center
//    CGPoint arcCenter = CGPointMake(size.width * 0.5, size.height * 0.5);
//    // -7.半径
//    CGFloat radius = image.size.width * 0.5 + margin * 0.5;
//    // -8.画边框
//    CGContextAddArc(ctx, arcCenter.x, arcCenter.y, radius, 0, 2 * M_PI, 1);
//    // -9.设置宽度
//    CGContextSetLineWidth(ctx, margin);
//    // -10.渲染边框
//    CGContextSetRGBStrokeColor(ctx, 70.0/255, 117.0/255, 168.0/255, 1.0);
//    CGContextStrokePath(ctx);
//    // -11.画需要裁剪的区域
//    CGContextAddArc(ctx, arcCenter.x, arcCenter.y, image.size.width * 0.5, 0, 2 * M_PI, 1);
//    // -12.裁剪
//    CGContextClip(ctx);
//    // -13.画头像
//    [image drawAtPoint:CGPointMake(margin, margin)];
//    // -14.获取当前image
//    image = UIGraphicsGetImageFromCurrentImageContext();
//    // -15.关闭图片上下文
//    UIGraphicsEndImageContext();
//    //保存截取的图片
//    NSString* docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
//    NSString* filePath = [docPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",@"jiawan"]];
//    // image 转化成 nsdata
//    NSData* data = UIImagePNGRepresentation(image);
//    // nsdata 去 写数据
//    [data writeToFile:filePath atomically:YES];
//    
//    // -16.赋值
//    [self.personButton  setBackgroundImage:image forState:UIControlStateNormal];
//    
//}


@end
